package com.hqully.smartfumehood.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.smartfumehood.common.vo.QueryVo;
import com.hqully.smartfumehood.entity.DevError;
import com.hqully.smartfumehood.entity.dto.DevErrorDto;
import com.hqully.smartfumehood.mapper.DeviceMapper;
import com.hqully.smartfumehood.mapper.LabManagerMapper;
import com.hqully.smartfumehood.mapper.UserMapper;
import com.hqully.smartfumehood.service.DevErrorService;
import com.hqully.smartfumehood.mapper.DevErrorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *     设备异常消息服务实现类
 * </p>
 * @author liulingyu
 * @date 2022-05-08 14:40
 * @version 1.0
 */
@Service
public class DevErrorServiceImpl extends ServiceImpl<DevErrorMapper, DevError>
    implements DevErrorService{

    @Autowired
    private DevErrorMapper  devErrorMapper;

    @Autowired
    private DeviceMapper deviceMapper  ;

    @Autowired
    private UserMapper userMapper  ;

    @Autowired
    private LabManagerMapper labManagerMapper  ;


    @Override
    public PageInfo<DevErrorDto> findAllDevError(QueryVo queryVo) {
        PageHelper.startPage(queryVo.getPageNum(),queryVo.getPageSize(),true);

        List<DevErrorDto> devErrorList = devErrorMapper.findAllDevError(queryVo.getQuery());

        PageInfo<DevErrorDto> devErrorPageInfo = new PageInfo<>(devErrorList);



        return devErrorPageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addDevError(DevError devError) {

        devErrorMapper.addDevError(devError);


        long unitId = deviceMapper.findUnitIdByDeviceId(devError.getDeviceId());

        List<Integer> adminUserIds = userMapper.findUserIdByRoleId(0);

        List<Integer> labmanageIds = labManagerMapper.findLabManagerIdByUnitId(unitId);

        List<Integer> UserIds = new ArrayList<Integer>();

        UserIds.addAll(adminUserIds);

        UserIds.addAll(labmanageIds);

        devErrorMapper.bindUsers(UserIds,devError.getId());



        return 0;
    }

    @Override
    public int deleteDevErrorById(Long id) {
        return 0;
    }

    @Override
    public int updateDevError(DevError devError) {
        return 0;
    }

    @Override
    public int haveRead(long id,String username) {

        return devErrorMapper.haveRead(id,username);
    }

    @Override
    public int haveProcessed(long id) {
        return devErrorMapper.haveProcessed(id);
    }

    @Override
    public int findUnreadCount(QueryVo queryVo) {
        return devErrorMapper.findUnread(queryVo.getQuery());
    }
}




